<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 灏忓 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\manage\controller;

use cmf\controller\AdminBaseController;
use app\portal\model\PortalPostModel;
use app\portal\service\PostService;
use app\portal\model\PortalCategoryModel;
use think\Db;
use app\admin\model\ThemeModel;

class NeedController extends AdminBaseController
{
	/**
	 * 椤圭洰鍒楄〃
	 * @adminMenu(
	 *     'name'   => '椤圭洰绠＄悊',
	 *     'parent' => 'portal/AdminIndex/default',
	 *     'display'=> true,
	 *     'hasView'=> true,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '椤圭洰鍒楄〃',
	 *     'param'  => ''
	 * )
	 * @return mixed
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function list()
	{
		
		$content = hook_one('portal_admin_meeting_index_view');
		
		if (!empty($content)) {
			return $content;
		}
		
		$field="*";
		$data=  db('need a')
		->field($field);
		
		$param = $this->request->param();
		$chain_id=isset($param['chain_id'])?$param['chain_id']:0;
		$chain_name=isset($param['chain_name'])?$param['chain_name']:'';
		
		$where="a.status!=-1 ";
		
		$keyword = $this->request->param('keyword', '', 'trim');
		
		
		
		if($keyword){
			$where.=" and title like '%$keyword%'";
		}
		
		
		$data=$data ->where($where)
		->order('list_order desc,id desc')
		->paginate(15,false,['query' => request()->param()]);
		$datares=$data->items();
		
		
		
		
		//p($datares);
		
		//p($datares);
		
		
		$this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');
		$this->assign('data', $datares);
		$this->assign('page', $data->render());
		
		
		return $this->fetch();
	}
	
	
	/**
	 * 娣诲姞椤圭洰
	 * @adminMenu(
	 *     'name'   => '娣诲姞椤圭洰',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> true,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '娣诲姞椤圭洰',
	 *     'param'  => ''
	 * )
	 * @return mixed
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function add()
	{
		
		
		return $this->fetch();
	}
	
	/**
	 * 娣诲姞椤圭洰鎻愪氦
	 * @adminMenu(
	 *     'name'   => '娣诲姞椤圭洰鎻愪氦',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '娣诲姞椤圭洰鎻愪氦',
	 *     'param'  => ''
	 * )
	 */
	public function addPost()
	{
		
		if ($this->request->isPost()) {
			$data = $this->request->param();
			
			
			
			$id=db('need')->insertGetId($data);
			
			
			
			$this->success('添加成功!', url('need/edit', ['id' => $id]));
		}
		
		
		
		
	}
	
	/**
	 * 缂栬緫椤圭洰
	 * @adminMenu(
	 *     'name'   => '缂栬緫椤圭洰',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> true,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '缂栬緫椤圭洰',
	 *     'param'  => ''
	 * )
	 * @return mixed
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function edit()
	{
		
		
		$id = $this->request->param('id', 0, 'intval');
		
		$post   = db('need')->where('id', $id)->find();
		
		
		$this->assign('post', $post);
		
		return $this->fetch();
	}
	
	/**
	 * 缂栬緫椤圭洰鎻愪氦
	 * @adminMenu(
	 *     'name'   => '缂栬緫椤圭洰鎻愪氦',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '缂栬緫椤圭洰鎻愪氦',
	 *     'param'  => ''
	 * )
	 * @throws \think\Exception
	 */
	public function editPost()
	{
		
		if ($this->request->isPost()) {
			$data = $this->request->param();
			
			
			
			db('need')->update($data);
			
			
			
			
			$this->success('修改成功!');
			
		}
	}
	
	/**
	 * 椤圭洰鍒犻櫎
	 * @adminMenu(
	 *     'name'   => '椤圭洰鍒犻櫎',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '椤圭洰鍒犻櫎',
	 *     'param'  => ''
	 * )
	 * @throws \think\Exception
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 * @throws \think\exception\PDOException
	 */
	public function delete()
	{
		$param           = $this->request->param();
		$portalPostModel = new PortalPostModel();
		
		if (isset($param['id'])) {
			$id           = $this->request->param('id', 0, 'intval');
			
			$resultPortal = db('need')
			->where('id', $id)
			->update(['status' =>-1]);
			if ($resultPortal) {
				$this->success("删除成功", '');
			}
			
			
		}
		
		if (isset($param['ids'])) {
			$ids     = $this->request->param('ids/a');
			$result  = db('need')->where('id', 'in', $ids)->update(['status' => -1]);
			if ($result) {
				
				$this->success("批量删除成功", '');
			}
		}
	}
	
	/**
	 * 椤圭洰鍙戝竷
	 * @adminMenu(
	 *     'name'   => '椤圭洰鍙戝竷',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '椤圭洰鍙戝竷',
	 *     'param'  => ''
	 * )
	 */
	public function publish()
	{
		$param           = $this->request->param();
		
		if (isset($param['ids']) && isset($param["yes"])) {
			$ids = $this->request->param('ids/a');
			db('need')->where('id', 'in', $ids)->update(['status' => 1]);
			$this->success("发布成功", '');
		}
		
		if (isset($param['ids']) && isset($param["no"])) {
			$ids = $this->request->param('ids/a');
			db('need')->where('id', 'in', $ids)->update(['status' => 0]);
			$this->success("取消发布成功", '');
		}
		
	}
	
	/**
	 * 椤圭洰缃《
	 * @adminMenu(
	 *     'name'   => '椤圭洰缃《',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '椤圭洰缃《',
	 *     'param'  => ''
	 * )
	 */
	public function top()
	{
		$param           = $this->request->param();
		
		if (isset($param['ids']) && isset($param["yes"])) {
			$ids = $this->request->param('ids/a');
			
			db('need')->where('id', 'in', $ids)->update(['top_time' => time()]);
			
			$this->success("置顶操作成功", '');
			
		}
		
		if (isset($_POST['ids']) && isset($param["no"])) {
			$ids = $this->request->param('ids/a');
			
			db('need')->where('id', 'in', $ids)->update(['top_time' => 0]);
			
			$this->success("置顶操作失败", '');
		}
	}
	
	/**
	 * 椤圭洰鎺ㄨ崘
	 * @adminMenu(
	 *     'name'   => '椤圭洰鎺ㄨ崘',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '椤圭洰鎺ㄨ崘',
	 *     'param'  => ''
	 * )
	 */
	public function recommend()
	{
		$param           = $this->request->param();
		$portalPostModel = new PortalPostModel();
		
		if (isset($param['ids']) && isset($param["yes"])) {
			$ids = $this->request->param('ids/a');
			
			$portalPostModel->where('id', 'in', $ids)->update(['recommended' => 1]);
			
			$this->success("鎺ㄨ崘鎴愬姛锛�", '');
			
		}
		if (isset($param['ids']) && isset($param["no"])) {
			$ids = $this->request->param('ids/a');
			
			$portalPostModel->where('id', 'in', $ids)->update(['recommended' => 0]);
			
			$this->success("鍙栨秷鎺ㄨ崘鎴愬姛锛�", '');
			
		}
	}
	
	/**
	 * 椤圭洰鎺掑簭
	 * @adminMenu(
	 *     'name'   => '椤圭洰鎺掑簭',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '椤圭洰鎺掑簭',
	 *     'param'  => ''
	 * )
	 */
	public function listOrder()
	{
		parent::listOrders(Db::name('need'));
		$this->success("操作成功", '');
	}
}
